package com.keensense.vcm.common.parser;

import static com.keensense.vcm.VCMErrorCode.SUCCESS;
import static com.keensense.vcm.VCMErrorCode.USER_NOT_LOGIN;
import static com.keensense.vcm.common.utils.VCMUtils.safeCloseResponse;

import java.lang.reflect.ParameterizedType;
import java.util.Map;
import java.util.Map.Entry;

import com.keensense.vcm.common.comm.ResponseMessage;
import com.keensense.vcm.common.utils.ExceptionFactory;
import com.keensense.vcm.common.utils.LogUtils;
import com.keensense.vcm.model.*;

/**
 * @author lanjian
 * @date 2020/4/29
 */
public final class ResponseParsers {

    public static final GlobalResponseParser<PeopleFaceSearchResponse> peopleFaceSearchResponseParser = new GlobalResponseParser(PeopleFaceSearchResponse.class);

    public static final GlobalResponseParser<VehicleSearchResponse> vehicleSearchResponseParser = new GlobalResponseParser(VehicleSearchResponse.class);

    public static final GlobalResponseParser<VCMResponse> loginResponseParser = new GlobalResponseParser(VCMResponse.class);

    public static final GlobalResponseParser<GetIntelligentAnalysisListResponse> getIntelligentAnalysisListResponseParser = new GlobalResponseParser(GetIntelligentAnalysisListResponse.class);

    public static final GlobalResponseParser<NvNResponse> nvNResponseParser = new GlobalResponseParser(NvNResponse.class);

    public static final GlobalResponseParser<OneVOneResponse> oneVOneResponseParser = new GlobalResponseParser(OneVOneResponse.class);

    public static final GlobalResponseParser<SnapInfosResponse> snapInfosResponseParser = new GlobalResponseParser(SnapInfosResponse.class);

    public static final GlobalResponseParser<BatchQueryProfileResponse> queryArchivesResponseParser = new GlobalResponseParser(BatchQueryProfileResponse.class);

    public static final GlobalResponseParser<QueryProfileLibrariesResponse> queryArchivesLibrariesResponseParser = new GlobalResponseParser(QueryProfileLibrariesResponse.class);

    public static final GlobalResponseParser<StatisticArchivesResponse> statisticArchivesResponseParser = new GlobalResponseParser(StatisticArchivesResponse.class);

    public static final GlobalResponseParser queryArchivesDetailResponseParser = new GlobalResponseParser(QueryProfileDetailResponse.class);

    public static final GlobalResponseParser<GetDataDictionaryResponse> getDataDictionaryResponseParser = new GlobalResponseParser(GetDataDictionaryResponse.class);

    public static final GlobalResponseParser<ManualCreateProfileResponse> manualCreateArchivesResponseParser = new GlobalResponseParser(ManualCreateProfileResponse.class);

    public static final GlobalResponseParser<ProfileQueryFacesResponse> profilePeopleFacesResponseParser = new GlobalResponseParser(ProfileQueryFacesResponse.class);

    public static final GlobalResponseParser<SpatiotemporalResponse> spatiotemporalResponseParser = new GlobalResponseParser(SpatiotemporalResponse.class);

    public static final GlobalResponseParser<SearchAlarmResponse> searchAlarmResponseParser = new GlobalResponseParser(SearchAlarmResponse.class);

    public static final GlobalResponseParser<SearchSuspectTaskResponse> searchSuspectResponseParser = new GlobalResponseParser(SearchSuspectTaskResponse.class);

    public static final GlobalResponseParser<GetPeoplesResponse> getPeoplesResponseParser = new GlobalResponseParser(GetPeoplesResponse.class);

    public static final GlobalResponseParser<ListStaticLibrariesResponse> listStaticLibrariesResponseParser = new GlobalResponseParser(ListStaticLibrariesResponse.class);

    public static final GlobalResponseParser<ListCameraResponse> listCameraResponseParser = new GlobalResponseParser(ListCameraResponse.class);

    public static final GlobalResponseParser<SubscriptionResponse> subscriptionResponseParser = new GlobalResponseParser(SubscriptionResponse.class);

    public static final GlobalResponseParser<SearchSubscriptionResponse> searchSubscriptionResponseParser = new GlobalResponseParser(SearchSubscriptionResponse.class);


    public static final class GlobalResponseParser<T> implements ResponseParser<T> {

        private Class<T> entityClass;

        public GlobalResponseParser(Class<T> entityClass) {
            this.entityClass = entityClass;
        }

        @Override
        public <T> T parse(ResponseMessage response) throws ResponseParseException {
            JAXBResponseParser<T> responseParser = new JAXBResponseParser(entityClass);
            T result = responseParser.parse(response);
            safeCloseResponse(response);
            return result;

        }
    }


    /**
     * 获取数据失败处理
     *
     * @param result
     */
    private static void unLoginHandler(VCMErrorResult result) {
        if (!SUCCESS.equals(result.getCode())) {
            LogUtils.getLog().error("errorCode=" + result.getCode() + ",errorMsg=" + result.getErrMsg());
        }
        if (USER_NOT_LOGIN.equals(result.getCode())) {
            throw ExceptionFactory.createVCMException(result, result.getErrMsg());
        }
    }
}
